/*
 * @Description: 
 * @Author: Zhang Zhangda
 * @Date: 2021-02-23 17:35:41
 * @LastEditTime: 2021-02-24 17:49:20
 * @LastEditors: Zhang Zhaoda
 */
import { FC } from 'react';
import { RouterFace } from "./router.d"
import { RouteProps } from "react-router"

import {
  Switch,
  Route,
  Redirect,
} from "react-router-dom";

interface Iprops {
  routes: RouterFace[]
}

let RouterView: FC<Iprops> = function ({ routes }) {
  let rou = routes.filter((item: RouterFace) => item.component);
  let red = routes.find((item: RouterFace) => item.redirect);
  return <Switch>
    {
      rou.map((item: RouterFace, index: number) => {
        return <Route
          key={index}
          path={item.path}
          render={
            (props: RouteProps) => {
              if (item.child) {
                return <item.component {...props} child={item.child}></item.component>
              };
              return <item.component {...props}></item.component>
            }
          }
        ></Route>
      })
    }
    {
      red ? <Redirect from={red.path} to={(red.redirect as string)}></Redirect> : <></>
    }
  </Switch>
}

export default RouterView;
